import random
import string
import time
import subprocess
from functools import partial

subprocess.Popen = partial(subprocess.Popen, encoding='utf-8')  # 这三行代码需要放在导入execjs之前
import execjs
import requests


def generate_random_string(length=5):
    # 定义字符集：小写字母 + 数字
    characters = string.ascii_lowercase + string.digits
    # 从字符集中随机选择length个字符
    random_string = ''.join(random.choice(characters) for _ in range(length))
    return random_string


cookies = {
    'HWWAFSESID': '2febb73d6ad7158fad',
    'HWWAFSESTIME': '1722426421246',
    'PHPSESSID': '25e467555bcd7b2ea8ab92f6ddf5a885cb549529',
    'time_diff': '-1',
    'XDEBUG_SESSION': 'XDEBUG_ECLIPSE',
    'login_referer': 'https%3A%2F%2Fwww.epwk.com%2F',
    'login_fail_need_graphics': '0',
}
timestamp = int(time.time())
NonceStr = str(timestamp) + generate_random_string()
headers = {
    'Accept': 'application/json, text/plain, */*',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Access-Token': '',
    'App-Id': '4ac490420ac63db4',
    'App-Ver': '',
    'CHOST': 'www.epwk.com',
    'Connection': 'keep-alive',
    'Content-Type': 'application/x-www-form-urlencoded',
    # 'Cookie': 'HWWAFSESID=2febb73d6ad7158fad; HWWAFSESTIME=1722426421246; PHPSESSID=25e467555bcd7b2ea8ab92f6ddf5a885cb549529; time_diff=-1; XDEBUG_SESSION=XDEBUG_ECLIPSE; login_referer=https%3A%2F%2Fwww.epwk.com%2F; login_fail_need_graphics=0',
    'Device-Os': 'web',
    'Device-Ver': '',
    'Imei': '',
    'NonceStr': NonceStr,
    'Origin': 'https://www.epwk.com',
    'Os-Ver': '',
    'Referer': 'https://www.epwk.com/login.html',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    # 'Signature': 'OibZHsgVx/ml4X9FSdlU88TeWvVA0aOKqSGJWasyrKAmlmh6/TPWZjs6W1+CIsS2',
    'Timestemp': str(timestamp),
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
    'X-REQUEST-ID': '487f7b22f68312d2c1bbc93b1aea445b',
    'sec-ch-ua': '"Not)A;Brand";v="99", "Google Chrome";v="127", "Chromium";v="127"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}
username = input('请输入用户名：')
password = input('请输入密码：')

data = {
    'username': username,
    'password': password,
    'code': '',
    'hdn_refer': 'https://www.epwk.com/',
}

c = 'a75846eb4ac490420ac63db46d2a03bf'
u = {
    "App-Ver": "",
    "Os-Ver": "",
    "Device-Ver": "",
    "Imei": "",
    "Access-Token": "",
    "Timestemp": timestamp,
    "NonceStr": NonceStr,
    "App-Id": "4ac490420ac63db4",
    "Device-Os": "web"
}
d = c + 'code' + 'hdn_referhttps://www.epwk.com/' + 'password' + password + 'username' + username + 'Access-TokenApp-Id4ac490420ac63db4App-VerDevice-OswebDevice-VerImeiNonceStr' + NonceStr + 'Os-VerTimestemp' + str(timestamp) + c
signature = execjs.compile(open('source.js').read()).call('h', u, data, c)
requestId = execjs.compile(open('source.js').read()).call('d', d)
print('signature:', signature)
print('requestId:', requestId)
headers['signature'] = signature
headers['X-REQUEST-ID'] = requestId
response = requests.post('https://www.epwk.com/api/epwk/v1/user/login', cookies=cookies, headers=headers, data=data)
print(response.text)
